package shuishijie.suishijie.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

/**
 * 网络相关常用方法
 */
public class WebUnit {

    public static boolean isNetworkAvailble(Context context) {
        boolean ret = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo;
        if (cm != null && (activeNetworkInfo = cm.getActiveNetworkInfo()) != null) {
            ret = activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
        }
        return ret;
    }

    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            NetworkInfo mWiFiNetworkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getNetworkInfo(1);
            if (mWiFiNetworkInfo != null && mWiFiNetworkInfo.isAvailable()) {
                return mWiFiNetworkInfo.isConnected();
            }
        }
        return false;
    }

    public static String getSSID(Context context) {
        String ssid = "";
        Context appContext = context.getApplicationContext();
        if (isWifiConnected(appContext)) {
            ssid = ((WifiManager) appContext.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getSSID();
        }
        if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }
        return ssid;
    }
}
